package com.charging.controller.app;


import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.charging.bean.User;
import com.charging.bean.layuiBean.ResultPage;
import com.charging.bean.layuiBean.VoPage;
import com.charging.config.config.BaseController;
import com.charging.config.result.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import static com.charging.config.result.ApiResult.fail;

/**
 * <p>
 * 个人信息 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-25
 */
@Controller
@RequestMapping("/app/user")
@Api(value = "用户登陆操作类", tags = {"用户登陆操作类"})
public class UserAppController extends BaseController {

    @ApiOperation(value = "登陆接口")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name="code",value="微信回调的code",dataType="string", paramType = "query"  )
    })
    public ApiResult getUser(String code, HttpSession session)   {
        if(StrUtil.isBlank(code)){
            return fail("code不能为空");
        }
        WxMaJscode2SessionResult result= userService.login(code);
        if(!StrUtil.isBlank(result.getSessionKey())&&!StrUtil.isBlank(result.getOpenid())){
                return userService.login(result,session);

        }
        return  fail("获取用户信息失败");

    }



    @ApiOperation(value = "更新用户信息接口")
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getUser(@RequestBody User user)   {
        System.out.println("user:"+user);
        if(StrUtil.isBlank(user.getId()+"")){
            return fail("id不能为空");
        }
        user.setMoeny("0");
        user.setOpenid(null);
        if(userService.updateById(user)){
            return ApiResult.ok();
        }
        return ApiResult.fail("更新用户失败");
    }



}
